import App from '@/App';
import store from '@/store/index';
import _ from 'lodash';
import { SET_MENUS, SET_ROUTES } from '@/store/constants';

import Layout from '@/layout';
import NotFound from '@/views/exception/notfound';
/* Router Modules */
import ExamplesRouter from './modules/examples';
import SnailRouter from './modules/snail';

/**
 * autoIndexRoute: 不显示在menu中
 * meta: { 附加信息
    title: 路由名称
    externaLink： 是否外链， 如果为true ，则打开新窗口跳转
    subMenu: Boolean  为true 标识 SubMenu菜单
    subNav: Boolean 为true 标识 三级菜单
  }
 */
export const childRoutes = [SnailRouter, ExamplesRouter];

function getAllMenuList({ menus, allMenuList, contentPath, subNavName }) {
  menus.forEach((node) => {
    if (!/^\//.test(node.path)) {
      if (contentPath === '/') {
        node.path = `/${node.path}`;
      } else {
        node.path = `${contentPath}/${node.path}`;
      }
    }
    const { childRoutes, ...data } = node;
    subNavName && (data.meta.subNavName = subNavName);
    allMenuList.push(data);

    const { subNav } = data.meta || {};
    let newSubNavName;
    if (subNav) {
      newSubNavName = node.name;
    }
    if (childRoutes && childRoutes.length > 0) {
      getAllMenuList({
        menus: childRoutes,
        allMenuList,
        contentPath: node.path,
        subNavName: newSubNavName,
      });
    }
  });
  return allMenuList;
}

const menus = () => {
  const routes = getAllMenuList({
    menus: childRoutes,
    allMenuList: [],
  });
  let menuData = [];
  childRoutes.forEach((item) => {
    const { childRoutes } = item;
    if (childRoutes.length > 0) {
      menuData = menuData.concat(childRoutes);
    }
  });
  store.dispatch({ type: SET_ROUTES, routes });
  store.dispatch({
    type: SET_MENUS,
    menus: {
      title: 'LEARN_EN',
      data: menuData,
    },
  });
};
menus();

const constantRoutes = [{ path: '/notfound', name: 'NotFound', component: NotFound }];
const routes = [
  {
    path: '/',
    component: App,
    childRoutes: [
      ...constantRoutes,
      {
        path: '*',
        name: 'Layout',
        component: Layout,
        childRoutes: [
          ...childRoutes.filter((r) => r.component || (r.childRoutes && r.childRoutes.length > 0)),
          // 设置根页面
          {
            path: '/',
            name: 'Home',
            redirect: '/snail/baseinfo',
            autoIndexRoute: true,
            exact: true,
          },
          // 这里设置 404 页面
          { path: '*', name: 'RedirectNotFound', autoIndexRoute: true, redirect: '/notfound' },
        ],
      },
    ],
  },
];

function handleIndexRoute(route) {
  if (!route.childRoutes || !route.childRoutes.length) {
    return;
  }

  const indexRoute = _.find(route.childRoutes, (child) => child.isIndex);
  if (indexRoute) {
    const first = { ...indexRoute };
    first.path = '';
    first.exact = true;
    first.autoIndexRoute = true; // mark it so that the simple nav won't show it.
    route.childRoutes.unshift(first);
  }
  route.childRoutes.forEach(handleIndexRoute);
}

routes.forEach(handleIndexRoute);
export default routes;
